<?php
class Request {
	
	protected $_filters;
	protected $_params;
	
	public function __construct() {
		$this->_filters = array();
		$this->_params = array();
	}
	
	/**
	 * 增加过滤器
	 * @param mixed $callback 回调过滤器函数
	 */
	public function addFilter($callback) {
		$this->_filters[] = $callback;
	}
	
	/**
	 * 应用过滤器
	 * @param mixed $value 要过滤的值
	 * @return mixed 过滤后的值
	 */
	public function applyFilter($value) {
		if (count($this->_filters)) {
			foreach ($this->_filters as $filter) {
				$value = is_array($value) ? array_map($filter, $value) 
						: call_user_func($filter, $value);
			}
		}
		return $value;
	}
	
	/**
	 * 魔术方法
	 * @param string $name GET或POST键名
	 * @retun 值
	 */
	public function __get($name) {
		return $this->get($name);
	}
	
	/**
	 * 返回键是否在GPC中
	 * @param string $name
	 * @return boolean
	 */
	public function __isset($name) {
		return (isset($_GET[$name]) || isset($_POST[$name]));
	}

	/**
	 * 获取GPC值
	 * @param string $name 键名
	 * @param mixed $default 默认值
	 * @return mixed 值
	 */
	public function get($name, $method = 'GP') {
		if ($method == 'G' || $method != 'P' && isset($_GET[$name])) {
			$gp = &$_GET;
		} else {
			$gp = &$_POST;
		}
		if (isset($gp[$name])) {
			$value = $gp[$name];
			$value = $this->applyFilter($value);
		} else {
			$value = null;
		}
		return $value;
	}
	
	public function getQuery($name, $default = null) {
		if (isset($_GET[$name])) {
			return $this->applyFilter($_GET[$name]);
		}
		return $default;
	}
	
	public function getPost($name, $default = null) {
		if (isset($_POST[$name])) {
			return $this->applyFilter($_POST[$name]);
		}
		return $default;
	}
	
	public function getParams() {
		return $this->_params;
	}
	
	public function addParams($name, $value = null) {
		if (is_array($name)) {
			$this->_params = array_merge($this->_params, $name);
		} else {
			$this->_params[$name] = $value;
		}
	}
	
	/**
	 * 获取客户端IP地址
	 * 
	 * @return string IP地址
	 */
	public function ip() {
		if ($_SERVER['HTTP_X_FORWARDED_FOR']) {
			$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
		} elseif ($_SERVER['HTTP_CLIENT_IP']) {
			$ip = $_SERVER['HTTP_CLIENT_IP'];
		} else {
			$ip = $_SERVER['REMOTE_ADDR'];
		}
		return preg_match("/^([0-9]{1,3}\.){3}[0-9]{1,3}$/", $ip) ? $ip : 'unknown';
	}

	/**
	 * 返回是否HTTP GET请求
	 */
	public function isGet() {
		return ($this->getServer('REQUEST_METHOD') == 'GET');
	}

	/**
	 * 返回是否HTTP POST请求
	 */
	public function isPost() {
		return ($this->getServer('REQUEST_METHOD') == 'POST');
	}
	
	/**
	 * 是否AJAX提交
	 *
	 */
	public function isAjax() {
		if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) {
			if('xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])) {
				return true;
			}
        }
        if($this->get('isajax')) return true;
        return false;
	}

	/**
	 * 获取$_SERVER环境变量值
	 * @param string $name 键名
	 * @param mixed $default 默认值
	 * @return mixed 值
	 */
	public function getServer($name, $default = NULL) {
		$value = isset($_SERVER[$name]) ? $_SERVER[$name] : $default;
		$value = $this->applyFilter($value);
		return $value;
	}
}